// pages/goods/detail/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: [],
    is_product: false,
    isChoose: false, //是否已选规格
    detail: '',
    num: "",
    sku: "",
    store_id: "",
    intro: "",
    ids: "",
    sku_product: "",
    is_user_show: true,
    is_user: 0,
    showUser: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    wx.removeStorageSync('items')
    that.getAddressList()
    app.http.post(app.api.goodsDetail, {
      id: options.id
    }, (res) => {
      let intro = res.data.goods.content.replace(/<img /g, '<img style="display:block" alt=""')
      that.setData({
        detail: res.data.goods,
        img: JSON.parse(res.data.goods.image_url),
        intro,
      })
    })
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)

  },
  buy() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
      return
    }
    const that = this
   
    app.http.post(app.api.goodsMakeOrder, {
      address_id: wx.getStorageSync('address').id,
      goods_id: this.data.detail.goods_id,
      num: this.data.num
    }, (res) => {
      wx.navigateTo({
        url: '/pages/goods/exchange/index?order_on=' + res.data.data.order_on,
      })
    })
  },
  getAddressList() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      return
    }
    wx.setStorageSync('address', {
      id: 0
    })
    wx.setStorageSync('add', 0)
    app.http.get(app.api.addressList, {}, (res) => {
      if (res.data.length > 0) {
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].is_default === 1) {
            wx.setStorageSync('address', res.data[i])
            break
          }
        }
      }

    })
  },
  close1(e) {
    console.log(11, e)
    this.setData({
      is_product: e.detail.is_product,
      isChoose: true,
      num: e.detail.num,
      sku: e.detail.sku,
      ids: e.detail.ids,
      sku_product: e.detail.sku_product
    });
  },
  open(e) {
    console.log(e)
    this.setData({
      is_product: true
    });
  },

})